package com.liu.audio.asr;

import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversation;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationOutput;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationParam;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationResult;
import com.alibaba.dashscope.common.MultiModalMessage;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.exception.UploadFileException;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 多模态模型
 */
public class MultiModel {

    public static String apiKey = "sk-6d51bedf84164e0087efbc2f52dec673";
    public static void main(String[] args) throws NoApiKeyException, UploadFileException {

        MultiModalConversation conv = new MultiModalConversation();
        MultiModalMessage userMessage = MultiModalMessage.builder()
                .role(Role.USER.getValue())
                .content(Arrays.asList(new HashMap<String, Object>() {{
                    put("audio", "file:///D:/5f421881-82eb-4887-9bc0-9447d925a178.pcm");
                }}))
                .build();



        MultiModalConversationParam param = MultiModalConversationParam.builder()
                .model("qwen-audio-asr-latest")
                .message(userMessage)
                .apiKey(apiKey)
                .build();
        MultiModalConversationResult result = conv.call(param);


        // 解析返回文本
        StringBuilder sb = new StringBuilder();
        List<MultiModalConversationOutput.Choice> choices = result.getOutput().getChoices();
        for (MultiModalConversationOutput.Choice choice : choices) {
            for (Map<String, Object> map : choice.getMessage().getContent()) {
                sb.append(map.get("text").toString());
            }
        }

        System.out.println(sb.toString());
    }
}
